package com.cenwangkeji.resident.ui.activity.special;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;

import com.cenwangkeji.resident.R;
import com.cenwangkeji.resident.model.Constant;
import com.cenwangkeji.resident.model.OkHttpUtils;
import com.cenwangkeji.resident.model.bean.User;
import com.cenwangkeji.resident.model.bean.XerBean;
import com.cenwangkeji.resident.ui.adapter.SpecialDiseaseDetailAdapter;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import com.lws.basic.base.BaseActivity;
import com.lws.basic.persenter.ObserverOnNext;
import com.lws.basic.utils.common.MyLogger;
import com.lws.basic.utils.common.MyToast;
import com.lws.basic.utils.common.RxUtils;
import com.lws.basic.view.TitleLayout;
import com.lws.basic.view.adapter.AdapterIntegerDefault0;
import com.lws.basic.view.adapter.DoubleDefault0Adapter;
import com.tencent.bugly.crashreport.CrashReport;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
//新生儿家庭访视详情
public class XerDetailActivity extends BaseActivity {


    @Override
    public int setLayoutId() {
        return R.layout.activity_xer_detail;
    }
    @BindView(R.id.title)
    TitleLayout mTitleLayout;
    @BindView(R.id.rv)
    RecyclerView mRv;
    private SpecialDiseaseDetailAdapter mAdapter;

    @Override
    public void initData() {
        super.initData();
        ButterKnife.bind(this);
        mTitleLayout.setText("新生儿家庭访视记录");
        initRecycelrView();
        getData();
    }

    private void initRecycelrView(){
        mRv.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new SpecialDiseaseDetailAdapter(null);
        mRv.setAdapter(mAdapter);
    }


    private void getData(){
        showLoading();
        Observable.create(new ObservableOnSubscribe<XerBean>() {
            @Override
            public void subscribe(ObservableEmitter<XerBean> emitter) throws Exception {
                int id = getIntent().getIntExtra("id", 0);
                Map<Object, Object> map = new HashMap<>();
                map.put("token", User.getToken());
                map.put("mark", 3);
                map.put("zxbId", id);
                JSONObject jsonObject = new JSONObject(map);
                MyLogger.i(jsonObject.toString());
                Gson gson = new GsonBuilder()
                        .registerTypeAdapter(Integer.class, new AdapterIntegerDefault0())
                        .registerTypeAdapter(int.class, new AdapterIntegerDefault0())
                        .registerTypeAdapter(Double.class, new DoubleDefault0Adapter())
                        .registerTypeAdapter(double.class, new DoubleDefault0Adapter())
                        .create();
                String json = OkHttpUtils.post(jsonObject.toString(),Constant.xer_detail);
                MyLogger.i(json);
                emitter.onNext(gson.fromJson(json, XerBean.class));
            }
        }).compose(RxUtils.<XerBean>io_main()).subscribe(new ObserverOnNext<XerBean>() {
            @Override
            public void onNext(XerBean bean) {
                try {
                    dismissLoading();
                    if (bean.getData() != null) {
                        List<String> list = new ArrayList<>();
                        list.add("姓名： " + bean.getData().getXm());
                        list.add("性别： " + bean.getData().getXb());
                        list.add("出生日期： " + bean.getData().getCsrq());
                        list.add("身份证号：  " + bean.getData().getSfz());
                        list.add("家庭住址：  " + bean.getData().getJtzz());
                        list.add("父亲名字：  " + bean.getData().getFxm());
                        list.add("职业：  " + bean.getData().getFzy());
                        list.add("出生日期：  " + bean.getData().getFcsrq());
                        list.add("电话：  " + bean.getData().getFdh());
                        list.add("母亲名字：  " + bean.getData().getMxm());
                        list.add("职业：  " + bean.getData().getMzy());
                        list.add("出生日期：  " + bean.getData().getMcsrq());
                        list.add("电话：  " + bean.getData().getMdh());
                        list.add("出生孕周：  " + bean.getData().getXseYj());
                        list.add("母亲妊娠期患病情况：  " + rzhbqk(bean.getData().getRcqhbqk()));
                        list.add("助产机构名称：  " + bean.getData().getZcjgmc());
                        if (!TextUtils.isEmpty(bean.getData().getCsqk())) {
                            list.add("出生情况：  " + csqk(Integer.valueOf(bean.getData().getCsqk())));
                        }
                        list.add("新生儿窒息： " + (bean.getData().getXseZx() == 1 ? "有" : "无"));
                        list.add("Apgar评分： " + bean.getData().getAqf());
                        list.add("畸形：  " + (bean.getData().getXseJx() == 1 ? "有" : "无"));
                        list.add("新生儿听力筛查： " + xertl(bean.getData().getXseTlsc()));
                        if (!TextUtils.isEmpty(bean.getData().getXseJbsc())) {
                            list.add("新生儿疾病筛查： " + xerjb(Integer.valueOf(bean.getData().getXseJbsc())));
                        }
                        list.add("新生儿出生体重：  " + bean.getData().getXseCstz() + "   kg");
                        list.add("目前体重：  " + bean.getData().getXseMqtz() + "   kg");
                        list.add("初生儿身长：  " + bean.getData().getXseCssz() + "   cm");
                        list.add("喂养方式：  " + wyfs(bean.getData().getXseWyfs()));
                        list.add("吃奶量：  " + bean.getData().getXseCnl() + "  mL/次");
                        list.add("吃奶次数:  " + bean.getData().getXseCncs() + " 次/日     呕吐：" + (bean.getData().getXseOt() == 1 ? "无" : "有"));
                        list.add("大便：  " + xerdb(bean.getData().getXseDb()));
                        list.add("大便次数:  " + bean.getData().getXseDbcs() + "  次/日");
                        list.add("体温:  " + bean.getData().getXseTw() + " ℃");
                        list.add("心率:  " + bean.getData().getXseXl() + " 次/分钟");
                        list.add("呼吸频率:  " + bean.getData().getXseHxpl() + " 次/分钟");
                        list.add("面色  " + ms(bean.getData().getXseMs(), bean.getData().getMsqt()));
                        if (!TextUtils.isEmpty(bean.getData().getXseHdbw())) {
                            list.add("黄疸部位:  " + hd(Integer.valueOf(bean.getData().getXseHdbw())));
                        }
                        list.add("前囟(cm):  " + qx(bean.getData().getXseQl()));
                        list.add("眼睛:  " + yc(bean.getData().getXseYj()));
                        list.add("四肢活动度:  " + yc(bean.getData().getXseSzhdd()));
                        list.add("耳外观:  " + yc(bean.getData().getXseEwg()));
                        list.add("颈部包块:  " + yc(bean.getData().getXseJbbk()));
                        list.add("鼻:  " + yc(bean.getData().getXseB()));
                        if (!TextUtils.isEmpty(bean.getData().getXsePf())) {
                            list.add("皮肤:  " + pf(Integer.valueOf(bean.getData().getXsePf())));
                        }
                        list.add("口腔:  " + yc(bean.getData().getXseKq()));
                        list.add("肛门:  " + yc(bean.getData().getXseGm()));
                        list.add("心肺听诊:  " + yc(bean.getData().getXseXftz()));
                        list.add("胸部:  " + yc(bean.getData().getXseXb()));
                        list.add("腹部触诊:  " + yc(bean.getData().getXseFbcz()));
                        list.add("脊柱:  " + yc(bean.getData().getXseJz()));
                        list.add("外生殖器:  " + yc(bean.getData().getXseWszq()));
                        if (!TextUtils.isEmpty(bean.getData().getXseQd())) {
                            list.add("脐带:  " + yc(Integer.valueOf(bean.getData().getXseQd())));
                        }
                        if (bean.getData().getZzjy() == 2) {// 1无 2有
                            list.add("转诊原因：" + bean.getData().getZzyx());
                            list.add("机构科室：" + bean.getData().getZzjgjks());
                        }
                        if (!TextUtils.isEmpty(bean.getData().getZd())) {
                            list.add("指导:  " + zd(Integer.valueOf(bean.getData().getZd())));
                        }
                        list.add("时间：" + bean.getData().getCreateDate());
                        mAdapter.setNewData(list);
                    } else {
                        MyToast.show(bean.getMsg());
                    }
                }catch (Exception e){
                     MyToast.show(e.getMessage());
                     CrashReport.postCatchedException(e);
                }
            }
        });

    }

    //指导
    private String zd(int index){
        String s ="";
        switch (index){
            case 1:
                s = "喂养指导";
                break;
            case 2:
                s = "发育指导";
                break;
            case 3:
                s = "防病指导";
                break;
            case 4:
                s = "预防伤害指导";
                break;
            case 5:
                s = "口腔保健指导";
                break;


        }
        return s;

    }

    //皮肤
    private String pf(int index){
        String s ="";
        switch (index){
            case 1:
                s = "未见异常";
                break;
            case 2:
                s = "湿疹";
                break;
            case 3:
                s = "糜烂";
                break;
            case 4:
                s = "其他";
                break;



        }
        return s;

    }
    //异常
    private String yc(int index){
        String s ="";
        switch (index){
            case 1:
                s = "未见异常";
                break;
            case 2:
                s = "异常";
                break;



        }
        return s;
    }



    //前囟
    private String qx(int index){
        String s ="";
        switch (index){
            case 1:
                s = "正常";
                break;
            case 2:
                s = "膨隆";
                break;
            case 3:
                s = "凹陷";
                break;
            case 4:
                s = "其他";
                break;


        }
        return s;
    }

    //黄疸部位
    private String hd(int index){
        String s ="";
        switch (index){
            case 1:
                s = "无";
                break;
            case 2:
                s = "面部";
                break;
            case 3:
                s = "躯干";
                break;
            case 4:
                s = "四肢";
                break;
            case 5:
                s = "手足";

        }
        return s;
    }

    //面色
    private String ms(int index ,String qt){
        String s ="";
        switch (index){
            case 1:
                s = "红润";
                break;
            case 2:
                s = "黄染";
                break;
            case 3:
                s = qt;
                break;

        }
        return s;
    }

    //喂养方式
    private String wyfs(int index){
        String s ="";
        switch (index){
            case 1:
                s = "纯母乳";
                break;
            case 2:
                s = "混合";
                break;
            case 3:
                s = "人工";
                break;

        }
        return s;
    }

    //大便
    private String xerdb(int index){
        String s ="";
        switch (index){
            case 1:
                s = "模糊";
                break;
            case 2:
                s = "稀";
                break;
            case 3:
                s = "其他";
                break;

        }
        return s;
    }

    //新生儿疾病
    private String xerjb(int index){
        String s ="";
        switch (index){
            case 1:
                s = "未进行";
                break;
            case 2:
                s = "检查均阴性";
                break;
            case 3:
                s = "甲低";
                break;
            case 4:
                s = "苯丙酮尿症";
                break;
            case 5:
                s = "其他遗传代谢病";
                break;
        }
        return s;
    }


    //新生儿听力
    private String xertl(int index){
        String s ="";
        switch (index){
            case 1:
                s = "通过";
                break;
            case 2:
                s = "未通过";
                break;
            case 3:
                s = "未筛查";
                break;
            case 4:
                s = "不详";
                break;
        }
        return s;
    }

    //出生情况
    private String csqk(int index){
        String s= "";
        switch (index){
            case 1:
                s = "顺产";
                break;
            case 2:
                s = "胎头吸引";
                break;
            case 3:
                s = "产钳";
                break;
            case 4:
                s = "刮宫";
                break;
            case 5:
                s = "产多胎";
                break;
            case 6:
                s = "臀位";
                break;
            case 7:
                s = "其他";
                break;

        }
        return s;
    }

    //母亲妊娠期患病情况
    private String rzhbqk(int index){
        String s ="";
        switch (index){
            case 1:
                s = "无";
                break;
            case 2:
                s = "糖尿病";
                break;
            case 3:
                s = "妊娠期高血压";
                break;
            case 4:
                s = "其他";
                break;
        }
     return s;
    }
}
